package com.gxa.modules.pc.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxa.common.utils.DateUtils;
import com.gxa.common.utils.Query;
import com.gxa.common.utils.TPage;
import com.gxa.modules.model.dto.ScheduleDTO;
import com.gxa.modules.model.entity.book.BookRegister;
import com.gxa.modules.model.entity.book.Schedule;
import com.gxa.modules.pc.order.mapper.ScheduleMapper;
import com.gxa.modules.pc.order.service.ScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生日程安排表 服务实现类
 * </p>
 *
 * @author zhuiyi
 * @since 2022-11-13 12:10:47
 */
@Service
@Slf4j
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Override
    public TPage queryByPage(Map<String, Object> searchMap) {
        //其他的查询参数
        String departmentId = (String) searchMap.get("departmentId");
        String doctorName = (String) searchMap.get("doctorName");
        String week = (String) searchMap.get("week");

        String orderField = (String) searchMap.get("orderField");
        String order = (String) searchMap.get("order");

        log.info("------departmentId----{}--doctorName---{}----week-{}-", departmentId, doctorName,week);

        if(StringUtils.isBlank(week)){
            week = "0";//没传默认是查本周的
        }
        //获取不同周的 开始日期和 结束日期
        Date[] weekStartAndEnd = DateUtils.getWeekStartAndEnd(Integer.parseInt(week));
        Date start = weekStartAndEnd[0];
        Date end = weekStartAndEnd[1];



        IPage<Schedule> oldPage = this.page(new Query<Schedule>().getPage(searchMap), new QueryWrapper<Schedule>()
                .eq(!StringUtils.isBlank(departmentId), "deparment_id", departmentId)
                .likeRight(!StringUtils.isBlank(doctorName), "doctor_name", doctorName)
                .between("work_date",start,end)
        );
        return new TPage(oldPage);
    }


    @Override
    public void deleteAndSave(Map<String, Object> map, List<ScheduleDTO> list) {
        String doctorName = (String) map.get("doctorName");
        String departmentId = (String) map.get("departmentId");

        /**
         * 查询并删除
         */
        List<Schedule> schedules = this.baseMapper.selectList((new QueryWrapper<Schedule>()
                .eq(!StringUtils.isBlank(doctorName), "doctor_name", doctorName)
                .eq(!StringUtils.isBlank(departmentId), "deparment_id", departmentId)
        ));

       for(Schedule schedule:schedules){
           this.baseMapper.deleteById(schedule.getId());
       }

        /**
         * 新增
         */
        Schedule schedule = new Schedule();
        for(ScheduleDTO dto:list){
            schedule.setDeparmentId(Long.parseLong(departmentId));
            schedule.setDoctorName(doctorName);

            schedule.setStatus(dto.getStatus());
            schedule.setWorkDate(DateUtils.stringToDate(dto.getWorkDate(),"yyyy-MM-dd"));
            schedule.setWorkTime(dto.getWorkTime());
            schedule.setReservedNumber(dto.getNum());
            schedule.setAvailableNumber(dto.getNum());
            schedule.setCreateTime(new Date());
            schedule.setUpdateTime(new Date());
            this.baseMapper.insert(schedule);
        }

    }
}
